<template>
  <div>
    <div>分配角色界面</div>
    <el-checkbox-group v-model="checkListone">
      <el-checkbox v-for="(item,index) in rolePageList" :key="index" :label="item.roleName" />
    </el-checkbox-group>
  </div>
</template>
<script>
import userManage from "@/api/system-settings/user-manage";
export default {
  props: {
    username: {
      type: String,
      default: ''
    },
    userstu: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      rolePageList: [],
      checkListone: [],
      hasrolelist: [],
      hasidlist: []
    }
  },
  watch: {
    userstu: {
      handler(Val) {
        if (Val === 'add') {
          this.checkchange()
        }
      }
    }
  },
  mounted() {
    this.getdatamet()
  },
  methods: {
    getdatamet() {
      userManage.getcheckboxdata().then(
        res => {
          this.rolePageList = res.data.content
          // console.log(res.data)
          this.getrollehas()
        }
      )
      console.log("获取数据")
    },
    getrollehas() {
      userManage.getrollehas(this.username).then(
        res => {
          this.hasrolelist = res.data || []
          let that = this
          if (this.hasrolelist) {
            this.hasrolelist.forEach(
              (item) => {
                this.rolePageList.forEach(
                  (itemtwo, indextwo) => {
                    if (itemtwo.roleNo === item.roleNo) {
                      that.hasidlist.push(item.roleNo)
                      that.checkListone.push(item.roleName)
                    }
                  }
                )
              }
            )
          }
          // console.log(res.data)
        }
      )
    },
    checkchange() {
      let that = this
      that.hasidlist = []
      if (this.checkListone.length > 0) {
        this.checkListone.forEach(
          (item) => {
            this.rolePageList.forEach(
              (itemtwo, indextwo) => {
                if (itemtwo.roleName === item) {
                  that.hasidlist.push(itemtwo.roleNo)
                }
              }
            )
          }
        )
      }
      // console.log(that.hasidlist, this.checkListone, this.username)
      let params = ``
      if (this.hasidlist.length > 0) {
        params = `roleNo=${this.hasidlist}&username=${this.username}`
        // roleUserMapperId = 12
      }
      userManage.addroleuth(params = `roleNo=${this.hasidlist}&username=${this.username}`).then(
        res => {
          if (res.code === 1) {
            this.$emit('users', '')
            this.$message.success("分配角色成功")
          } else {
            this.$message.error(`${res.msg}`)
          }
        }
      )
      // console.log(this.hasidlist)
      // console.log(this.checkListone)
    }
  }
}
</script>
